package com.yunxi.controller.qrtz;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.quartz.pojo.JobAndTriggerRespVO;
import com.yunxi.quartz.service.JobAndTriggerDubboService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Api(tags = "qrtz:job",description = "定时任务管理接口")
@RestController
@RequestMapping("/qrtz/job")
public class JobController {
//    @Reference(version = "${demo.service.version}",
//            application = "${dubbo.application.id}",
//            registry = "${dubbo.registry.address}",
//            url = "${dubbo.application.url}",
//            check = false,
//            timeout = 10000
//    )
    @Autowired
    private JobAndTriggerDubboService jobAndTriggerDubboService;

    @PostMapping(value = "/addjob")
    public void addjob(@RequestParam(value = "jobClassName") String jobClassName,
                       @RequestParam(value = "jobGroupName") String jobGroupName,
                       @RequestParam(value = "cronExpression") String cronExpression) throws Exception {
        jobAndTriggerDubboService.addJob(jobClassName, jobGroupName, cronExpression);
    }

    /**
     * 暂停任务
     *
     * @param jobClassName
     * @param jobGroupName
     * @throws Exception
     */
    @PostMapping(value = "/pausejob")
    public void pausejob(@RequestParam(value = "jobClassName") String jobClassName, @RequestParam(value = "jobGroupName") String jobGroupName) throws Exception {
        jobAndTriggerDubboService.jobPause(jobClassName, jobGroupName);
    }

    /**
     * 恢复任务
     *
     * @param jobClassName
     * @param jobGroupName
     * @throws Exception
     */
    @PostMapping(value = "/resumejob")
    public void resumejob(@RequestParam(value = "jobClassName") String jobClassName, @RequestParam(value = "jobGroupName") String jobGroupName) throws Exception {
        jobAndTriggerDubboService.jobresume(jobClassName, jobGroupName);
    }
	
	@PostMapping(value="/reschedulejob")
	public void rescheduleJob(@RequestParam(value="jobClassName")String jobClassName,
			@RequestParam(value="jobGroupName")String jobGroupName,
			@RequestParam(value="cronExpression")String cronExpression) throws Exception {
        jobAndTriggerDubboService.jobreschedule(jobClassName, jobGroupName, cronExpression);
    }

	@PostMapping(value="/deletejob")
	public void deletejob(@RequestParam(value="jobClassName")String jobClassName, @RequestParam(value="jobGroupName")String jobGroupName) throws Exception {
        jobAndTriggerDubboService.jobdelete(jobClassName, jobGroupName);
    }

	@GetMapping(value="/queryjob")
	public ApiResult<JobAndTriggerRespVO> queryjob(@RequestParam(value="pageNum")Integer pageNum, @RequestParam(value="pageSize")Integer pageSize)
	{
		PageInfo<JobAndTriggerRespVO> jobAndTriggerRespVOPageInfo = jobAndTriggerDubboService.getJobAndTriggerDetails(pageNum, pageSize);
		return ResponseUtil.ok(jobAndTriggerRespVOPageInfo);
	}
}
